//
//  HSHomeWorkTableViewController.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/9/26.
//  Copyright © 2016年 黄华强. All rights reserved.
//

import UIKit
import SVProgressHUD

class HSHomeWorkTableViewController: UITableViewController {

    var dataArray:[HSHomeWorkTableModel] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(HSHomeWorkTableViewCell.self, forCellReuseIdentifier: "hwCell")
        
        tableView.rowHeight = 68
        
        loadData()
    }

     func loadData() {
        let url = "ZH-schoolname-S-getHomeworkBase"
        let key = userInfo["key"] as! String
        let param = ["key":key,"id":0] as [String : Any]
        
        
        HSNetWorkTools.shared.downloadData(url: url, parameters: param) { [weak self] (data, error) in
            
            guard let data = data as? NSDictionary else {
                SVProgressHUD.showInfo(withStatus: "网络请求错误！")
                return
            }

            let dictArr = data["data"] as! [[String:Any]]
            for dict in dictArr {
                let model = HSHomeWorkTableModel(dict: dict)
                self?.dataArray.append(model)
            }
            self?.tableView.reloadData()
        }
        
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "hwCell", for: indexPath) as! HSHomeWorkTableViewCell
        cell.model = dataArray[indexPath.row]
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let Dvc = HSDetailsViewController()
        Dvc.id = dataArray[indexPath.row].hw_id
        navigationItem.backBarButtonItem = UIBarButtonItem(title: dataArray[indexPath.row].hw_title, style: .done, target: nil, action: nil)
        Dvc.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(Dvc, animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

   
}
